package com.chanyachting.emall.action.applicationLeter;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.beanutils.BeanUtils;
import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import com.chanyachting.emall.domain.Applicationleter;
import com.chanyachting.emall.domain.Jobhistory;
import com.chanyachting.emall.form.JobHistoryForm;
import com.chanyachting.emall.service.ApplicationLeterService;
import com.chanyachting.emall.serviceImpl.ApplicationLeterServiceImpl;
import com.chanyachting.emall.util.RequestUtil;

public class AddJobHistoryAction extends Action {

	@Override
	public ActionForward execute(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		
		JobHistoryForm jhForm=(JobHistoryForm)form;
		
		Jobhistory	jh=new Jobhistory();
		
		BeanUtils.copyProperties(jh, jhForm);
		
		RequestUtil ru=new RequestUtil(request);
		
		
		//如果有求职信息对象编号
		if(jhForm.getAppId()!=0){
			
			Applicationleter al=new Applicationleter();
			
			al.setId(jhForm.getAppId());
			
			ApplicationLeterService alservice=new ApplicationLeterServiceImpl();
			
			boolean temp=alservice.addJobHistory(al, jh);
			
			if(temp) ru.setSuccessMsg(); else ru.setFailMsg();
			
		}else{
			
			//从sesinon 中获得已经保存过的工作历史对象集合
			List jhList=(List)ru.getSessionObject(ApplicationLeterService.JOBHISTORYLISTINSESSINO, new ArrayList());
			
			jh.setId(0);
			
			jhList.add(jh); //保存工作经历对象到集合中
			
			//将所有工作经历对象，保存在session 中
			ru.setSessionObject(ApplicationLeterService.JOBHISTORYLISTINSESSINO, jhList);
			
			ru.setSuccessMsg();
			
		}
		
		return mapping.findForward("");
		
		//return mapping.findForward("addJobHistory");
	}
	
	
	

}
